package com.h3bpm.web.controller;

import OThinker.H3.Controller.ControllerBase;
import OThinker.H3.Controller.RestfulApi.cloudpivot.ResponseResult;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.h3bpm.web.service.ProductOutsourceService;
import data.DataException;
import data.DataRow;
import data.DataRowCollection;
import data.DataTable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 新版工序委外流程
 */
@RestController
@RequestMapping("/api/productOutsource")
public class ProductOutsourceController extends ControllerBase {
    private static final Logger logger = LoggerFactory.getLogger(ProductOutsourceController.class);

    @Autowired
    private ProductOutsourceService productOutsourceService;

    @RequestMapping("/getOutsourceLibrary")
    public  ResponseResult getOutsourceLibrary(String outsourceType,String outsourcingNo,
                                      String trainModal, String productName,String figureModel, String applicationUnit){
        ResponseResult rst = new ResponseResult(-1L,"",null);
        String sql = "select * from outsourceprojectlibrary2022 where 1=1 ";
//        if(StringUtils.isNoneBlank(outsourceType)){
//            sql += String.format(" and OutsourceType = '%s' ", outsourceType);
//        }
//        if(StringUtils.isNoneBlank(outsourcingNo)){
//            sql += String.format(" and OutsourcingNo = '%s' ", outsourcingNo);
//        }
//        if(StringUtils.isNoneBlank(trainModal)){
//            sql += String.format(" and TrainModal = '%s' ", trainModal);
//        }
//        if(StringUtils.isNoneBlank(productName)){
//            sql += String.format(" and ProductName = '%s' ", productName);
//        }
//        if(StringUtils.isNoneBlank(figureModel)){
//            sql += String.format(" and FigureModel = '%s' ", figureModel);
//        }
//        if(StringUtils.isNoneBlank(applicationUnit)){
//            sql += String.format(" and ApplicationUnit = '%s' ", applicationUnit);
//        }
        logger.info("委外项目库查询sql = {}",sql);
        DataTable dataTable = getEngine().getPortalQuery().QueryTable(sql);
        logger.info("委外项目库查询结果 = {}",dataTable);
        if (dataTable != null && dataTable.getRows() != null && dataTable.getRows().size() > 0) {
            logger.info("委外项目库查询结果数量 = {}",dataTable.getRows().size());
            DataRowCollection rows = dataTable.getRows();
            List<Map<String, Object>> maps = new ArrayList<>();
            try {
                for (int i = 0; i < rows.size(); i++) {
                    Map<String, Object> resMap = new HashMap<>();
                    DataRow dataRow = rows.get(i);
                    String id = dataRow.getString("id");
                    resMap.put("id",id);
                    maps.add(resMap);
                }
                rst.setData(maps);
                rst.setErrcode(0L);
                rst.setErrmsg("成功");
                logger.info("委外项目库查询返回值 = {}", maps);
            } catch (DataException e) {
                rst.setErrmsg(e.getMessage());
                e.printStackTrace();
            }
        }
        return rst;
    }

    @RequestMapping("/updateExecutableTrue")
    public ResponseResult updateExecutableTrue(String objectId){
        logger.info("开始更改工序委外流程为可出门状态,流程objectId：{}",objectId);
        ResponseResult rst = new ResponseResult(-1L,"",null);
        boolean bool = productOutsourceService.updateExecutableTrue(objectId);
        if (bool){
            rst.setErrmsg("成功");
            rst.setErrcode(0L);
            logger.info("更改工序委外流程为可出门状态成功,流程objectId：{}",objectId);
        }else {
            rst.setErrmsg("失败");
            logger.info("更改工序委外流程为可出门状态失败,流程objectId：{}",objectId);
        }
        return rst;
    }

    @RequestMapping("/updateExecutableFalse")
    public ResponseResult updateExecutableFalse(String objectId){
        logger.info("开始更改工序委外流程为不可出门状态,流程objectId：{}",objectId);
        ResponseResult rst = new ResponseResult(-1L,"",null);
        boolean bool = productOutsourceService.updateExecutableFalse(objectId);
        if (bool){
            rst.setErrmsg("成功");
            rst.setErrcode(0L);
            logger.info("更改工序委外流程为不可出门状态成功,流程objectId：{}",objectId);
        }else {
            rst.setErrmsg("失败");
            logger.info("更改工序委外流程为不可出门状态失败,流程objectId：{}",objectId);
        }
        return rst;
    }

    /**
     *  出门数据验证
     * @param objectId
     * @return
     */
    @RequestMapping("/processOutValid")
    public Object processOutValid(String objectId){

        JSONObject obj = new JSONObject();

        try {
            logger.debug("objectId:{}",objectId);

            long l1 = System.currentTimeMillis();

            Object o = productOutsourceService.processOutValid(objectId);

            long l3 = System.currentTimeMillis();
            if (logger.isDebugEnabled()){
                logger.info("service 服务时长:{}",l3-l1);
            }

            obj.put("validProcessOutFlag",o);


        } catch (Exception e) {
            e.printStackTrace();
            obj.put("validProcessOutFlag","系统异常");
        }


        return obj;
    }
    @RequestMapping("/setProcessInDate")
    public Object setProcessInDate(String objectId){
        long l1 = System.currentTimeMillis();
        logger.info("开始更新工序委外流程主数据信息,流程objectId：{}",objectId);
        JSONObject obj = new JSONObject();
        Object object = productOutsourceService.updateProcessIn(objectId);
        obj.put("validFlag",object);
        long l3 = System.currentTimeMillis();
        if (logger.isDebugEnabled()){
            logger.info("入库service 服务时长:{}",l3-l1);
        }
        return obj;
    }

    /**
     *  结算数据校验并回写
     * @param objectId
     * @return
     */
    @RequestMapping("settleDatavalid")
    public Object settleDatavalid(String objectId){

        logger.info("结算回写开始");
        JSONObject obj = new JSONObject();
        logger.debug("objectId:{}",objectId);

        long l1 = System.currentTimeMillis();
        Object o = productOutsourceService.settleDataWriteBack(objectId);

        long l3 = System.currentTimeMillis();
        if (logger.isDebugEnabled()){
            logger.info("service 服务时长:{}",l3-l1);
        }

        obj.put("validProcessOutFlag",o);

        return obj;
    }
    @RequestMapping("processInDetails")
    public Object processInDetails(String objectId,String outsourceTitle,String partnerUnit,String applicationUnitId){
        boolean bool = productOutsourceService.processInDetails(objectId, outsourceTitle, partnerUnit, applicationUnitId);
        return bool;
    }


    @RequestMapping("monthlySettlementDetails")
    public Object monthlySettlementDetails(String objectId,String partnerUnit,String applicationUnitId){
        boolean bool = productOutsourceService.monthlySettlementDetails(objectId, partnerUnit, applicationUnitId);
        return bool;
    }

    @RequestMapping("processInCleanUp")
    public Object processInCleanUp(String objectId){
        boolean bool = productOutsourceService.processInCleanUp(objectId);
        return bool;
    }

    @RequestMapping("monthlySettlementCleanUp")
    public Object monthlySettlementCleanUp(String objectId){
        boolean bool = productOutsourceService.monthlySettlementCleanUp(objectId);
        return bool;
    }

    @Override
    public String getFunctionCode() {
        return null;
    }
}
